/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validar;

import javax.faces.component.*;
import javax.faces.context.*;
import java.util.regex.*;
import javax.faces.application.FacesMessage;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;



/**
 *
 * @author ruanda
 */
public class validationEmail implements Validator {

    public validationEmail() {
    }

    
    @Override
    public void validate(FacesContext facesContext, UIComponent uIComponent, Object object) throws ValidatorException {
        String enteredEmail = (String) object;
        Pattern p = Pattern.compile("^[a-zA-Z0-9_-]{1,}(.|[a-zA-Z0-9_-]{2,})*@[a-zA-Z0-9]{2,}\\.[a-zA-Z]{2,3}(\\.[a-zA-Z]{2})?$");
        Matcher m = p.matcher(enteredEmail);
        boolean matchFound = m.matches();

        if (matchFound==false && enteredEmail.isEmpty()== false) {
            FacesMessage message = new FacesMessage();
            message.setSummary("E-mail invalido .");
            throw new ValidatorException(message);
        }


    }
}
